//
//  TraveMainViewController.m
//  课余时间
//
//  Created by zgr on 16/4/8.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "TraveMainViewController.h"
#import "TraveMainModel.h"
#import "TraveMainCollectionViewCell.h"
#import "RequestManager.h"
#import "UIColor+AddColor.h"
#import "TraveMianDetailViewController.h"
@interface TraveMainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *allArray;
@property (nonatomic,strong)UIView *cityView;
@property (nonatomic,strong)UIButton *CNbutton;
@property (nonatomic,assign)BOOL isAppear;

@end

@implementation TraveMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _modelArray = [NSMutableArray array];
    _allArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;

    
    self.navigationItem.title = @"国内.大陆";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[TraveMainCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [self requstDataWithUrl:TRAVE_Main];
    
    _cityView = [[UIView alloc]initWithFrame:CGRectMake(20, -150, KWIDTH - 40, 150)];
    _cityView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_cityView];
    _cityView.alpha = 0.8;
    NSArray *cityArray = @[@"国外.亚洲",@"国外.欧洲",@"国外.美、澳、非",@"国内.港澳台"];
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(20 + 140* j, 10 + 40 * i, 130, 30);
            [button setTitle:cityArray[ i *2 + j] forState:UIControlStateNormal];
            button.tag = 1000 + i * 2 + j;
            button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
            [button setTintColor:[UIColor blackColor]];
            [_cityView addSubview:button];
            [button addTarget:self action:@selector(changCity:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
   _CNbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    _CNbutton.frame = CGRectMake(100, 100,_cityView.frame.size.width - 200, 30);
    [_CNbutton setTitle:@"国内.大陆" forState:UIControlStateNormal];
    _CNbutton.tag = 1004;
     _CNbutton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [_CNbutton setTintColor:[UIColor emerlandColor]];
   // _CNbutton.backgroundColor = [UIColor greenColor];
    [_cityView addSubview:_CNbutton];
    [_CNbutton addTarget:self action:@selector(changCity:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithTitle:@"城市" style:UIBarButtonItemStylePlain target:self action:@selector(city:)];
    self.navigationItem.rightBarButtonItem = barButton;
    
}
-(void)city:(UIBarButtonItem *)barButton
{
    if (_isAppear == NO) {
        [UIView animateWithDuration:1 animations:^{
            _cityView.frame = CGRectMake(20, 0, KWIDTH - 40, 150);
        }];
        
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            _cityView.frame = CGRectMake(20, -150, KWIDTH - 40, 150);
        }];
    }
    _isAppear = !_isAppear;
    
    
    
}

-(void)changCity:(UIButton *)button
{
    for (UIButton *cityButton in _cityView.subviews) {
        [cityButton setTintColor:[UIColor blackColor]];
        
    }
    [_CNbutton setTintColor:[UIColor blackColor]];

    [button setTintColor:[UIColor emerlandColor]];
    self.modelArray = _allArray[button.tag - 1000];
    [_collectionView reloadData];
    self.navigationItem.title = [button titleForState:UIControlStateNormal];
    
    
    
}
-(void)requstDataWithUrl:(NSString *)urlString
{
    
    
    [[[RequestManager alloc]init] requestWithUrl:urlString requestType:RequestTypeGet parDic:nil finish:^(NSData *data) {
        
        NSArray *bigArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
     //   NSLog(@"----%@",bigArray);
        
        for (NSDictionary *dataDic in bigArray) {
            NSArray *destinations = [dataDic objectForKey:@"destinations"];
            NSMutableArray * desArray = [NSMutableArray array];
            
            
            for (NSDictionary *destionDic in destinations) {
                TraveMainModel *model = [[TraveMainModel alloc]init];
                [model setValuesForKeysWithDictionary:destionDic];
                [desArray addObject:model];
            }
           
            [_allArray addObject:desArray];
           
        }
        
dispatch_async(dispatch_get_main_queue(), ^{
            
            if (_allArray.count != 0) {
                _modelArray = _allArray[4];
            }
            [_collectionView reloadData];
        
        });
        
    } error:^(NSError *error) {
        
    }];
    
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    TraveMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    TraveMainModel *model = _modelArray[indexPath.row];
 //   cell.model = model;
    [cell setCellWithModel:model];
   // cell.backgroundColor = [UIColor redColor];
    return cell;
    
}
// 点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    TraveMainModel *model = _modelArray[indexPath.item];
    TraveMianDetailViewController *trvD = [[TraveMianDetailViewController alloc]init];
    trvD.desID = model.traveMaimID;
    [self.navigationController pushViewController:trvD animated:YES];

    
    
    
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
    
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KWIDTH - 15)/2, (KWIDTH - 15)/2 * 352 / 272);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
